package com.hsav.kcxb.view;

import android.content.Context;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Layout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hsav.kcxb.R;

import java.util.ArrayList;

/**
 * Created by zhao on 2017/2/4.
 */

public class GiftLayoutView extends FrameLayout {

    private String[] mTitleList = {"礼物","贡献榜","收到的礼物"};
    private View mRootView;
    private TabLayout mTitleTabLayout;
    private ViewPager mViewPager;

    public GiftLayoutView(Context context) {
        this(context,null);
    }

    public GiftLayoutView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public GiftLayoutView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        mRootView = layoutInflater.inflate(R.layout.frame_gift_layout,this);
        mTitleTabLayout = (TabLayout)mRootView.findViewById(R.id.tablayout_gift);
        mViewPager = (ViewPager)mRootView.findViewById(R.id.viewpager_gift);
        mTitleTabLayout.setupWithViewPager(mViewPager);
        final ArrayList<View> viewList = new ArrayList<View>();
//        TextView tv = new TextView(context);
//        tv.setText("one");
//        viewList.add(tv);
        GiftItemView giftItemView = new GiftItemView(context);
        viewList.add(giftItemView);
//        TextView tvTwo = new TextView(context);
//        tvTwo.setText("two");
        ContributeView contributeView = new ContributeView(context);
        viewList.add(contributeView);
        TextView tvThree = new TextView(context);
        tvThree.setText("three");
        viewList.add(tvThree);
        mViewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mTitleList.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(viewList.get(position));
                return viewList.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View)object);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTitleList[position];
            }
        });
    }


}
